<?php

abstract class Resource
{
    protected $cache;
    protected $resourceManager;

    public function __construct( ResourceManager $resourceManager )
    {
        $this->resourceManager = $resourceManager;
        $this->cache = $resourceManager->GetCache();
    }

    public function GetTTL()
    {
        return 0;
    }

    public function GetData()
    {
        $data = null;
        if ( is_null($this->cache) )
        {
            $data = $this->GetFreshData();
        }
        else
        {
            $id = $this->GetId();
            $data = $this->cache->Get($id);
            if ( is_null($data) )
            {
                $data = $this->GetFreshData( true );
                if ( is_null($data) )
                {
                    $data = $this->cache->Get($id, true);
                }
                else
                {
                    $this->cache->Set($id, $data, $this->GetTTL());
                }
            }
        }
        return $data;
    }

    public abstract function GetId();
    public abstract function GetFreshData();
}

?>
